// TypeScript file
class StarlingSwfFactory
{
    private static mInstance: StarlingSwfFactory;
    /**
     * 单利
     */
    public static getInstance(): StarlingSwfFactory
    {
        if (null == StarlingSwfFactory.mInstance)
        {
            StarlingSwfFactory.mInstance = new StarlingSwfFactory();
        }

        return StarlingSwfFactory.mInstance;
    }

    private swfAssetsManager: starlingswf.SwfAssetManager;
    private swfAssetsNames: Array<string>;
    private swfAssets: Array<starlingswf.Swf>;
    private swfData: any;

    public constructor()
    {
        this.swfAssetsManager = new starlingswf.SwfAssetManager();
        this.swfAssetsNames = new Array<string>();
        this.swfAssets = new Array<starlingswf.Swf>();
        this.swfData = {};
    }

    public addSwf(name: string, swfData: Object, spriteSheet: egret.SpriteSheet): void
    {
        if (this.swfAssetsNames.indexOf(name) != -1)
        {
            return;
        }

        if (null == swfData || null == spriteSheet)
        {
            console.log("SWF加载失败:"+name);
            return;
        }

        this.swfAssetsManager.addSpriteSheet(name, spriteSheet);
        let swf: starlingswf.Swf = new starlingswf.Swf(swfData, this.swfAssetsManager, 24);
        swf.name = name;
        //
        StarlingSwfUtils.addSwf(swf);
        this.swfAssetsNames.push(name);
        this.swfAssets.push(swf);
    }

    public stopSwfs(arr: Array<string>): void
    {
        for (let i: number = 0, len: number = arr.length; i < len; ++i)
        {
            let swf: starlingswf.Swf = StarlingSwfUtils.getSwf(arr[i]);
            if (null == swf)
            {
                continue;
            }
            swf.swfUpdateManager.stop();
        }
    }

    public playSwfs(arr: Array<string>): void
    {
        for (let i: number = 0, len: number = arr.length; i < len; ++i)
        {
            let swf: starlingswf.Swf = StarlingSwfUtils.getSwf(arr[i]);
            if (null == swf)
            {
                continue;
            }
            swf.swfUpdateManager.play();
        }
    }

    public clear(): void
    {
        this.clearSwfs();
    }

    private clearSwfs(): void
    {
        while (this.swfAssets.length)
        {
            StarlingSwfUtils.removeSwf(this.swfAssets.pop());
        }

        while (this.swfAssetsNames.length)
        {
            this.swfAssetsNames.pop();
        }

        this.swfAssetsManager = new starlingswf.SwfAssetManager();
    }

    public makeMc(name: string): StarlingSwfMovieClip
    {
        let mc: StarlingSwfMovieClip = <StarlingSwfMovieClip>StarlingSwfUtils.createMovie("mc_"+name, null, StarlingSwfMovieClip);
        if (null == mc)
        {
            console.log("SWF创建失败: " + name);
        }
        return mc;
    }

    public makeImage(name: string): egret.Bitmap
    {
        return StarlingSwfUtils.createImage("img_"+name);
    }

    public getTexture(name: string): egret.Texture
    {
        return StarlingSwfUtils.getTexture("img_"+name);
    }
}